package com.thermal.thermal_inspection.controller;

import com.thermal.thermal_inspection.common.CommonResponse;
import com.thermal.thermal_inspection.entity.NfcManage;
import com.thermal.thermal_inspection.service.MapService;
import com.thermal.thermal_inspection.service.NfcService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/NFC")
public class NfcController {
    private static final Logger logger = Logger.getLogger(ClassController.class);
    @Autowired
    private NfcService nfcService;
    @Autowired
    private MapService mapService;
    /**
     * 新建NFC信息
     */
    @PostMapping("")
    public CommonResponse<String>  newNfcManage(@RequestBody NfcManage nfcManage){
        System.out.println("nfcManage:"+nfcManage);
        if (nfcService.getNfcManageByNfcName(nfcManage.getNfcName()) == null){
            Integer isWrong = null;
            try {
                isWrong = nfcService.newNFCManage(nfcManage);
            } catch (Exception e) {
                logger.error("NFC信息已录入！");
                String nfcName = nfcService.getNfcManageByNfcMsg(nfcManage.getNfcMsg()).getNfcName();
                return CommonResponse.createForError("NFC信息已录入！已存在的NFC的名字:"+nfcName);
            }
            if (isWrong < 1){
                logger.error("NFC信息插入失败！");
                return CommonResponse.createForError("NFC信息插入失败！");
            }else {
                logger.info("NFC信息插入成功！");
                return CommonResponse.createForSuccess("NFC信息插入成功！");
            }
        }
        else {
            logger.info("NFC名字重复！");
            return CommonResponse.createForError("NFC名字重复！");
        }
    }
    /**
     * 获取NFC所有信息
     */
    @GetMapping("")
    public CommonResponse<List<NfcManage>> getNfcManages(){
        List<NfcManage> nfcManages = nfcService.getAllNfcManages();
        if (nfcManages.isEmpty()){
            logger.error("NFC信息为空！");
            return CommonResponse.createForError("NFC信息为空！");
        }else {
            logger.info("获取NFC信息成功！");
            return CommonResponse.createForSuccess(nfcManages);
        }
    }
    /**
     * 修改NFC名字
     */
    @PutMapping("")
    public CommonResponse<String> updateNfcName(@RequestBody NfcManage nfcManage){
        NfcManage nfcManage1 = nfcService.getNfcManageByNfcMsg(nfcManage.getNfcMsg());
        if (nfcManage1 == null){
            logger.error("NFC信息不存在！");
            return CommonResponse.createForError("NFC信息不存在！");
        }else {
            if(nfcService.isRepeatOfName(nfcManage.getNfcName())){
                logger.error("NFC名字已存在！");
                return CommonResponse.createForError("NFC名字已存在！");
            }else{
                nfcService.updateNfcManage(nfcManage);
                logger.info("NFC信息已修改!");
                return CommonResponse.createForSuccess("NFC信息修改成功！");
            }
        }
    }
    /**
     * 删除NFC信息
     */
    @DeleteMapping("/{nfcMsg}")
    public CommonResponse<String> deleteNfcManage(@PathVariable String nfcMsg){
        String selectMsg = nfcService.isUseNFC(nfcMsg);
        if (selectMsg != null)
            return CommonResponse.createForError(selectMsg);
        if (nfcService.getNfcManageByNfcMsg(nfcMsg) == null){
            logger.error("未找到NFC信息！");
            return CommonResponse.createForError("未找到要删的的NFC信息，请刷新！");
        }else{
            nfcService.deleteNfcManage(nfcMsg);
            mapService.updateNFCToEmpty(nfcMsg);
            logger.error("删除NFC信息成功！");
            return CommonResponse.createForSuccess("删除NFC信息成功！");
        }
    }
}
